package cn.virens.server.netty_tcp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.virens.common.BytesBuilder;
import cn.virens.server.netty_tcp.packet.TcpWritePacket;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DatagramPacketEncoder;
import io.netty.handler.codec.MessageToByteEncoder;

@SuppressWarnings("unused")
public class TCPServerPacketEncoder extends MessageToByteEncoder<TcpWritePacket> {
	private Logger logger = LoggerFactory.getLogger(DatagramPacketEncoder.class);

	private final TCPServerPacketHandler tCPServerPacketHandler;

	public TCPServerPacketEncoder(TCPServerPacketHandler tCPServerPacketHandler) {
		this.tCPServerPacketHandler = tCPServerPacketHandler;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, TcpWritePacket packet, ByteBuf out) throws Exception {
		// 用户数据区 ----------------------------------START
		BytesBuilder userData = new BytesBuilder();
		userData.append(packet.seqByte());
		userData.append(packet.lenByte());
		userData.append(packet.cmdByte());
		userData.append(packet.dataByte());

		// 发送数据
		out.writeBytes(userData.toArray());
	}
}
